package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.common.constant.BusinessKey;
import cn.itsource.ymcc.common.constant.RConst;
import cn.itsource.ymcc.common.result.JsonResult;
import cn.itsource.ymcc.common.util.AssertUtil;
import cn.itsource.ymcc.common.util.BitStatesUtils;
import cn.itsource.ymcc.domain.Login;
import cn.itsource.ymcc.domain.User;
import cn.itsource.ymcc.dto.UserRegisterDto;
import cn.itsource.ymcc.mapper.UserMapper;
import cn.itsource.ymcc.openfeign.LoginFeignClient;
import cn.itsource.ymcc.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author zcj
 * @since 2023-11-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private LoginFeignClient loginFeignClient;
    @Override
//    @Transactional
    @GlobalTransactional(rollbackFor = Exception.class)
    public JsonResult register(UserRegisterDto userRegisterDto) {
        String smsKey =  userRegisterDto.getMobile()+ BusinessKey.SEGMENTATION+ BusinessKey.REGISTER_SMS_CODE_SUFFIX;
        AssertUtil.isTrue(redisTemplate.hasKey(smsKey),"短信验证已过期");
        String smsCode = userRegisterDto.getSmsCode();

        String string = redisTemplate.opsForValue().get(smsKey).toString();
        AssertUtil.isTrue(smsCode.equalsIgnoreCase(string),"短信验证码不匹配");
        //判断手机号是否已经被注册
        User userInfo = baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPhone, userRegisterDto.getMobile()));
        AssertUtil.isNull(userInfo,"该用户已被注册");

        //保存login注册信息
        Login login = new Login();
        login.setPassword(new BCryptPasswordEncoder().encode(userRegisterDto.getPassword()));
        login.setType(userRegisterDto.getRegChannel());
        login.setUsername(userRegisterDto.getMobile());
        login.setEnabled(RConst.Num.INT_1);
        JsonResult jsonResult = loginFeignClient.saveOrUpdate(login);
        if(!jsonResult.getSuccess()){//远程调用失败的情况
           return JsonResult.error(jsonResult.getMessage());
        }
        //能不能拿的到？  绝对拿不到
        //login.getId();
        Object id = jsonResult.getData();//通过openFeign进行远程调用所有的long类型都会转成integer类型

        //中间来报错
        int i = 1/0;

        //保存user注册信息
        User user = new User();
        user.setPhone(userRegisterDto.getMobile());
        user.setNickName(userRegisterDto.getMobile());
        user.setCreateTime(new Date());
        user.setLoginId(((Integer) id).longValue());
//        user.setLoginId();
        Long bitState = BitStatesUtils.addState(0L, BitStatesUtils.OP_REGISTED);
        Long l = BitStatesUtils.addState(bitState, BitStatesUtils.OP_AUTHED_PHONE);
        //toBinaryString 转为二进制格式
        user.setBitState(Long.valueOf(Long.toBinaryString(l)));
        baseMapper.insert(user);
        return JsonResult.success();
    }
}
